﻿// -------------------------------------------------------------------------
//    @FileName         :    NFWhiteListCache.cpp
//    @Author           :    gaoyi
//    @Date             :    24-8-15
//    @Email            :    445267987@qq.com
//    @Module           :    NFWhiteListCache
//
// -------------------------------------------------------------------------

#include "NFWhiteListCache.h"

bool operator==(const tbWriteList &lhs, const tbWriteList &rhs)
{
    return lhs.account_id == rhs.account_id && lhs.account_pwd == rhs.account_pwd;
}

NFWhiteListCache::NFWhiteListCache()
{
    if (EN_OBJ_MODE_INIT == NFShmMgr::Instance()->GetCreateMode())
    {
        CreateInit();
    }
    else
    {
        ResumeInit();
    }
}

NFWhiteListCache::~NFWhiteListCache()
{
}

int NFWhiteListCache::CreateInit()
{
    return 0;
}

int NFWhiteListCache::ResumeInit()
{
    return 0;
}

int NFWhiteListCache::AddWhiteList(proto_ff::tbWriteList &oWhiteList)
{
    CHECK_EXPR(!m_oHashTable.full(), -1, "m_oHashTable.full, oWhiteList:{}", oWhiteList.Utf8DebugString());
    tbWriteList stWhiteList;
    stWhiteList.FromPb(oWhiteList);
    CHECK_EXPR(!IsInWhiteList(&stWhiteList), -1, "oWhiteList:{} exist", oWhiteList.Utf8DebugString());
    m_oHashTable.insert(stWhiteList);
    return 0;
}

int NFWhiteListCache::DelWhiteList(proto_ff::tbWriteList &oWhiteList)
{
    tbWriteList stWhiteList;
    stWhiteList.FromPb(oWhiteList);

    int iRetCode = m_oHashTable.erase(stWhiteList);
    CHECK_EXPR(iRetCode > 0, iRetCode, "delete item failed:{}", iRetCode);

    return 0;
}

bool NFWhiteListCache::IsInWhiteList(tbWriteList *pstWhiteList)
{
    CHECK_EXPR(pstWhiteList, false, "");
    if (m_oHashTable.find(*pstWhiteList) != m_oHashTable.end())
    {
        return true;
    }
    return false;
}
